//
//  AddUserViewController.m
//  myselfTest
//
//  Created by Lucifer on 15-5-21.
//  Copyright (c) 2015年 xuxzhou. All rights reserved.
//

#import "AddUserViewController.h"
#import "User.h"
#import "userDB.h"
@interface AddUserViewController ()
@property(nonatomic,strong)UITextField * userNameTF;
@property(nonatomic,strong)UITextView * headerURlTF;
@property (nonatomic, strong) userDB * userDB;
@property(nonatomic,strong)User * user;

@end

@implementation AddUserViewController
-(id)init{
    self =[super init];
    if (self) {
        self.user=nil;
    }
    return self;
}
- (id) initWithUser:(User *) user {
    self = [super init];
    if (self) {
        self.user = user;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.userDB = [[userDB alloc] init];
    // Do any additional setup after loading the view.
    if (!self.user) {
        UIBarButtonItem * left = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(dismiss:)];
        self.navigationItem.leftBarButtonItem = left;
    } else {
        self.navigationItem.title = [NSString stringWithFormat:@"用户ID:%@",self.user.uid];
    }
    UIBarButtonItem * right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:@selector(done:)];
    self.navigationItem.rightBarButtonItem = right;

    UILabel * label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 60, 40)];
    label1.text = @"姓名";
    label1.backgroundColor = [UIColor clearColor];
    [self.view addSubview:label1];
    
    self.userNameTF = [[UITextField alloc] initWithFrame:CGRectMake(80, 40, 200, 40)];
    self.userNameTF.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.userNameTF];
    self.userNameTF.layer.borderColor = [UIColor darkGrayColor].CGColor;
    self.userNameTF.layer.cornerRadius = 10.0;
    self.userNameTF.layer.masksToBounds = YES;
    
    
    UILabel * label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 120, 60, 40)];
    label2.text = @"描述";
    label2.backgroundColor = [UIColor clearColor];
    [self.view addSubview:label2];
    
    self.headerURlTF = [[UITextView alloc] initWithFrame:CGRectMake(80, 120, 200, 80)];
    [self.view addSubview:self.headerURlTF];
    self.headerURlTF.layer.borderColor = [UIColor darkGrayColor].CGColor;
    self.headerURlTF.layer.cornerRadius = 10.0;
    self.headerURlTF.layer.masksToBounds = YES;
    
    if (self.user) {
        self.userNameTF.text = _user.name;
        self.headerURlTF.text = _user.headerImageUrlStr;
    }
}

- (void) done:(id) sender {
    
    if (self.user) {
        _user.name = self.userNameTF.text;
        _user.headerImageUrlStr = self.headerURlTF.text;
        [_userDB mergeWithUser:self.user];//改
        if (self.delegate &&[self.delegate respondsToSelector:@selector(addOrChangeUser:AtIndexPath:)]) {
            [self.delegate addOrChangeUser:_user AtIndexPath:_indexPath];
        }
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        User * user = [User new];
        user.name = self.userNameTF.text;
        user.headerImageUrlStr = self.headerURlTF.text;
        [_userDB saveUser:user];//增加
        if (self.delegate &&[self.delegate respondsToSelector:@selector(addOrChangeUser:AtIndexPath:)]) {
            [self.delegate addOrChangeUser:user AtIndexPath:_indexPath];
        }
       [self.navigationController popViewControllerAnimated:YES];
    }

    
}

- (void) dismiss:(id) sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
